﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VINETLIFE.BL;
using VINETLIFE.DO;
using System.Data;

namespace VINETLIFE.AdminCP
{
    public partial class Category : System.Web.UI.Page
    {
        cmsCategoryDO objCat = new cmsCategoryDO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                bindToDropDown(cboCategory);
            if (Request.QueryString["CategoryID"] != null)
            {
                objCat.CategoryID = int.Parse(Request.QueryString["CategoryID"].ToString());
                if (!IsPostBack)
                    initForm();
            }
        }
        public void bindToDropDown(DropDownList ddl)
        {
            DataTable dtCate = new cmsCategoryBL().SelectAll(); //new DataView(new cmsMenuBL().SelectAll(),"IsContent = 0","",DataViewRowState.CurrentRows).ToTable();
            foreach (DataRow dr in dtCate.Rows)
            {
                if (dr["ParentID"].ToString().Equals("0"))
                {
                    ListItem item = new ListItem();
                    item.Value = dr["CategoryID"].ToString();
                    item.Text = "- " + dr["Title"].ToString();
                    ddl.Items.Add(item);
                    GetChildItems(dr["CategoryID"].ToString(), dtCate, ddl);
                }
            }
        }
        private void GetChildItems(string parentID, DataTable dtTemp, DropDownList ddl)
        {
            foreach (DataRow dr in dtTemp.Rows)
            {
                if (dr["ParentID"].ToString().Equals(parentID))
                {
                    ListItem listChild = new ListItem();
                    listChild.Text = Server.HtmlDecode("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;") + "+ " + dr["Title"].ToString();
                    listChild.Value = dr["CategoryID"].ToString();
                    listChild.Attributes.Add("style", "background:#f9f9f9;color:#648293;");
                    ddl.Items.Add(listChild);
                }
            }
        }
        private void initForm()
        {
            objCat = new cmsCategoryBL().Select(objCat);
            txtTitle.Text = objCat.Title;
            txtDescription.Text = objCat.Description;
            Session["Iscontent"] = objCat.IsContent ? "true" : "false";
            // txtDetailContent.Text = objCat.DetailContent;
            txtDetail.Content = objCat.DetailContent;
            chkIsHomePage.Checked = objCat.IsHompage;
            chkIsPublish.Checked = !objCat.IsPublish;
            txtPageTitle.Text = objCat.PageTitle;
            txtMetaTag.Text = objCat.MetaTag;
            txtMetaDes.Text = objCat.MetaDes;
            //if (objCat.CategoryTypeID == 0) rdoSV.Checked = true;
            //else if (objCat.CategoryTypeID == 1) rdoSVD.Checked = true;
            //else if (objCat.CategoryTypeID == 2) rdoCT.Checked = true;
            //else if (objCat.CategoryTypeID == 3) rdoNW.Checked = true;

            if (objCat.IsContent == true)
                rdoCT.Checked = true;
            else
                rdoTT.Checked = true;

            cboCategory.SelectedValue = objCat.ParentID.ToString();
            txtOrderID.Text = objCat.OrderID.ToString();
            if (!string.IsNullOrEmpty(objCat.ImageURL))
            {
                hplImage.NavigateUrl = "~/Media/" + objCat.ImageURL;
                hplImage.Visible = true;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            initObject();
            if (objCat.CategoryID <= 0)
            {
                objCat.CreateDate = DateTime.Now;
                objCat.UserCreate = 1;//int.Parse(Session["UserID"].ToString());
                new cmsCategoryBL().Insert(objCat);
            }
            else
            {
                new cmsCategoryBL().Update(objCat);
            }
            if (objCat.IsContent)
                Functions.Alert("Cập nhật thành công!", "ListCategory.aspx?Type=1");
            else
                Functions.Alert("Cập nhật thành công!", "ListCategory.aspx?Type=0");
        }

        private void initObject()
        {
            objCat = new cmsCategoryBL().Select(objCat);
            objCat.Title = txtTitle.Text;
            objCat.CreateDate = DateTime.Now;
            objCat.Description = txtDescription.Text;
            //objCat.DetailContent = txtDetailContent.Text;
            objCat.DetailContent = txtDetail.Content;
            objCat.IsHompage = chkIsHomePage.Checked;
            objCat.IsPublish = !chkIsPublish.Checked;
            objCat.MetaDes = txtMetaDes.Text;
            objCat.MetaTag = txtMetaTag.Text;
            objCat.PageTitle = txtPageTitle.Text;
            //if (rdoCT.Checked) objCat.CategoryTypeID = 2;
            //else if (rdoSV.Checked) objCat.CategoryTypeID = 0;
            //else if (rdoNW.Checked) objCat.CategoryTypeID = 3;
            //else if (rdoSVD.Checked) objCat.CategoryTypeID = 1;

            if (rdoCT.Checked)
                objCat.IsContent = true;
            else if (rdoTT.Checked)
                objCat.IsContent = false;

            objCat.OrderID = int.Parse(txtOrderID.Text);

            objCat.ParentID = int.Parse(cboCategory.SelectedValue);

            if (!string.IsNullOrEmpty(fuImage.FileName))
                objCat.ImageURL = UploadFile(fuImage);
        }

        private string UploadFile(FileUpload fulImage)
        {
            if (!string.IsNullOrEmpty(fulImage.FileName))
            {
                string FileName = string.Format("{0}{1}", Functions.Change_AV(txtTitle.Text) + "-" + DateTime.Now.ToString("ddMMyyyyhhmmss"), fulImage.FileName.Substring(fulImage.FileName.LastIndexOf(".")));
                string SaveLocation = string.Format("{0}\\{1}", Server.MapPath("~/Media/"), FileName);
                fulImage.SaveAs(SaveLocation);
                return FileName;
            }
            return string.Empty;
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            objCat.IsContent=bool.Parse(Session["Iscontent"].ToString());
            if (objCat.IsContent)
            Response.Redirect("ListCategory.aspx?Type=1");
            else
                Response.Redirect("ListCategory.aspx?Type=0");
        }
    }
}